<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head th:replace="~{common::header('制造中心采购计划编制')}"></head>
<body>
<div class="container-fluid">
    <div class="row clearfix">
        <div class="col-md-12 column" style="text-align: center">
            <h3>
                未编采购计划的需求一览表
            </h3>
        </div>
    </div>

    <div class="row clearfix">
        <div class="col-md-12 column">
            <hr>
        </div>
    </div>

    <div class="row clearfix">
        <div class="col-md-12 column">
            <div class=" col-md-3 column form-inline">
            </div>
            <div class="col-sm-6 col-md-3 column form-inline">
                <label for="a1">产品编码:</label>
                <input type="text" class="form-control" id="a1" placeholder="产品编码">
            </div>
            <div class="col-sm-6 col-md-3 column form-inline">
                <label for="a2">产品名称:</label>
                <input type="text" class="form-control" id="a2" placeholder="产品名称">
            </div>

            <div class="col-sm-12 col-md-3 column">
                <button type="button" class="btn btn-primary" onclick="btn2()" style="margin-right: 10px">搜索</button>
                <button type="button" class="btn btn-primary" onclick="btn3()" >重置</button>
            </div>

        </div>
    </div>

    <div class="row clearfix">
        <div class="col-md-12 column">
            <hr>
        </div>
    </div>
    <div class="row clearfix" style="max-height: 50%;overflow-y: auto;overflow-x: hidden">
        <div class="row clearfix">
            <div class="col-md-12 column">
                <div class="col-md-12 column" style="overflow: hidden">
                    <table id="dg" class="easyui-datagrid"
                           data-options="
								  	pageSize:'10',
								  	pageList:[10,20,30,40],
									pagination:true,
									rownumbers:true,
									url:'/orders/findMaterialPage2',
									method:'get',
									width:'100%'">
                        <thead>
                        <tr>
                            <th data-options="field:'ck',checkbox: true"></th>
                            <th data-options="field:'ID'" hidden>Item ID(15%)</th>
                            <th data-options="field:'MATERIAL_CODE',width:'19%',sortable:true,order:'desc'">物资编码</th>
                            <th data-options="field:'MATERIAL_NAME',width:'19%'">物资名称</th>
                            <th data-options="field:'AMOUNT',width:'19%',sortable:true">物资数量</th>
                            <th data-options="field:'SUM_PRICE',width:'19%',sortable:true,order:'desc'">总金额</th>
                            <th data-options="field:'END_DATE',width:'19%',sortable:true,order:'desc'">需求时间</th>
                        </tr>
                        </thead>
                    </table>
                </div>
            </div>
        </div>
    </div>


    <div class="row clearfix">
        <div class="col-md-12 column">
            <hr>
        </div>
    </div>

    <div class="col-md-12 column">
        <div class="col-md-12 column" style="padding-top:10px;text-align:right">
            <button type="button" onclick="btn4()" style="float: left"  class="btn btn-primary" >选择同类</button>
            <button type="button" onclick="btn5()"  class="btn btn-primary" >编制采购计划</button>
        </div>
    </div>

</div>


<script>


    //模糊查询
    var btn2=function (){
        var a1=$("#a1").val();
        var a2=$("#a2").val();
        $('#dg').datagrid({
            url:'/orders/findMaterialPage2',
            method:'get',
            queryParams: {
                materialCode:a1,
                materialName:a2,
            }
        });
    }

    //重置搜索数据
    var btn3=function(){
        $("#a1").val("");
        $("#a2").val("");
    }

    var btn4=function () {
        var rows = $('#dg').datagrid('getSelections');
        var pd=false;
        if (rows.length==1){
            pd=true;
        }else if (rows.length!=0){
            var num=rows[0].MATERIAL_CODE;
            pd=true;
            for (var i = 1; i < rows.length; i++) {
                   if (num!=rows[i].MATERIAL_CODE){
                       pd=false;
                   }
            }
        }

        if (pd){
            var row=$('#dg').datagrid('getSelected');
            if (row!=null){
                var rowsa=$('#dg').datagrid('getRows');
                for (var i = 0; i <rowsa.length ; i++) {
                    if (row.MATERIAL_CODE==rowsa[i].MATERIAL_CODE){
                        $('#dg').datagrid('checkRow',$('#dg').datagrid('getRowIndex',rowsa[i]));
                    }
                }
            }
        }

    }

    var btn5=function (){
        var rows=$('#dg').datagrid('getSelections');
        if (rows.length!=0){
            var materialcode=rows[0].MATERIAL_CODE;
            pd=true;
            for (var i = 1; i <rows.length ; i++) {
                if (materialcode!=rows[i].MATERIAL_CODE){
                    pd=false;
                }
            }

            if (pd){
                var temp = document.createElement("form")
                temp.action = "/orders/next";
                temp.method = "post";
                temp.style.display = "none";
                for(var i=0; i<rows.length; i++){
                    var opt = document.createElement("input");
                    opt.name = "nums";
                    opt.value =rows[i].ID;
                    temp.appendChild(opt);
                }
                document.body.appendChild(temp);
                temp.submit();
            }else{
                alert("不能选择不相同类型的产品");
            }
        }
    }

</script>


</body>

</html>